#include "GeneralSetup.h"


GeneralSetup::GeneralSetup(void)
{
}


GeneralSetup::~GeneralSetup(void)
{
}


void GeneralSetup::parseSetupFile(string setupFile)
{
	path pathSetupFile(setupFile);
	try{
		if (exists(pathSetupFile))    // does p actually exist?
		{
			if (is_regular_file(pathSetupFile))      // is p a directory?
			{
				ifstream ifs;
				//cout<<pathSetupFile.file_string()<<endl;
				ifs.open(pathSetupFile.c_str());
				if(ifs.is_open())
				{
					string line="";
					while(getline(ifs,line))
					{
						if(line.length()>0)
						{
							if(int pos = line.find("dosegrid:") != string::npos){
								string dosegrid = line.substr(pos+string("dosegrid:").length());
								trim(dosegrid);
								this->dosegridFile = dosegrid;
							}else if(int pos = line.find("fieldinfo:") != string::npos){
								string fieldinfo = line.substr(pos+string("fieldinfo:").length());
								trim(fieldinfo);
								erase_all(fieldinfo," ");
								erase_all(fieldinfo,"\t");
								erase_all(fieldinfo,"dij=");
								erase_all(fieldinfo,"weight=");
								string dij = fieldinfo.substr(0,fieldinfo.find(";"));
								string weight = fieldinfo.substr(fieldinfo.find(";")+1);
								Field field;
								field.dijFile = dij;
								field.weightFile = weight;
								this->fieldFiles.push_back(field);
							}else if(int pos = line.find("structure:") != string::npos){
								string structure = line.substr(pos+string("structure:").length());
								trim(structure);
								Structure s;
								if(int pos = structure.find(";EUD")!=string::npos){
									string file = structure.substr(0,structure.find(";EUD"));
									s.name=file.substr(0,file.find("="));
									s.structureFile=file.substr(file.find("=")+1);

									string eudS = structure.substr(structure.find(";EUD")+string(";EUD").length());
									s.eud=atof(eudS.substr(eudS.find("=")+1).c_str());
								}else{
									s.name=structure.substr(0,structure.find("="));
									s.structureFile=structure.substr(structure.find("=")+1);
								}
								
								this->structureFiles.push_back(s);
							}else if(int pos = line.find("objective:") != string::npos){
								string objective = line.substr(pos+string("objective:").length());
								trim(objective);
								SConstraint cons;
								vector<string> strs;
								boost::split(strs,objective,boost::is_any_of(","));
								cons.strStructureName = strs[0];
								cons.constraintType=strs[1];
								cons.aimDose=atof(strs[2].c_str());
								cons.aimWeight=atof(strs[3].c_str());
								this->objectives.push_back(cons);
							}else if(int pos = line.find("outputDir:") != string::npos){
								string outputDir = line.substr(pos+string("outputDir:").length());
								trim(outputDir);
								this->outputDir=outputDir;
							}else if(int pos = line.find("maxIter:") != string::npos){
								string maxIter = line.substr(pos+string("maxIter:").length());
								trim(maxIter);
								this->maxIter=atoi(maxIter.c_str());
							}
						}
					}
				}
			}
			else{
				cout << pathSetupFile << " exists, but is not a regular file\n";
			}
		}
		else
			cout << pathSetupFile << " does not exist\n";
	}
	catch(const filesystem_error& ex){
		cout<<ex.what()<<endl;
	}
}